<template>
  <div>
    <el-form ref="form" label-width="90px">
      <el-form-item label="SPU名称">
        {{ spuName }}
      </el-form-item>
      <el-form-item label="SKU名称">
        <el-input v-model="skuInfo.skuName" placeholder="SKU名称" />
      </el-form-item>
      <!-- 使用umber属性将输入框变成数字选择模式 -->
      <el-form-item label="价格(元)">
        <el-input v-model="skuInfo.price" placeholder="价格(元)" type="number" />
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input v-model="skuInfo.weight" placeholder="重量(千克)" />
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input v-model="skuInfo.skuDesc" type="textarea" placeholder="规格描述" rows="4" />
      </el-form-item>
      <el-form-item label="平台属性">
        <!-- 行内from -->
        <el-form ref="form" :inline="true" label-width="80px">
          <el-form-item v-for="item in attrInfoList" :key="item.id" :label="item.attrName">
            <el-select v-model="item.attrIdandValueId" placeholder="请选择">
              <el-option v-for="item2 in item.attrValueList" :key="item2.id" :label="item2.valueName" :value="`${item.id}:${item2.id}`" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form ref="form" :inline="true" label-width="80px">
          <el-form-item v-for="item in spuSaleAttrList" :key="item.id" :label="item.saleAttrName">
            <el-select v-model="item.attrIdandValueId" placeholder="请选择">
              <el-option v-for="item2 in item.spuSaleAttrValueList" :key="item2.id" :label="item2.saleAttrValueName" :value="`${item.id}:${item2.id}`" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table
          style="width: 100%"
          border
          :data="spuImageList"
          @selection-change="change"
        >
          <template slot="empty">
            <el-empty description="空" />
          </template>
          <el-table-column
            prop="prop"
            label="label"
            type="selection"
            width="80"
          />
          <el-table-column
            prop="prop"
            label="图片"
            width="width"
          >
            <template slot-scope="{row}">
              <div>
                <el-image
                  style="width: 100px; height: 100px"
                  :src="row.imgUrl"
                  fit="fit"
                />
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="imgName"
            label="名称"
            width="width"
          />
          <el-table-column
            prop="prop"
            label="操作"
            width="width"
          >
            <template slot-scope="{row}">
              <div>
                <el-button v-if="row.isDefault===0" type="primary" @click="changeDef(row)">设置默认</el-button>
                <el-tag v-else type="success">默认</el-tag>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      spuName: '',
      // 存入图片信息
      spuImageList: [],
      // 销售属性
      spuSaleAttrList: [],
      // 平台属性
      attrInfoList: [],

      // 收集SKU数据
      skuInfo: {
        // 父组件给的数据
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        // v-model收集
        price: 0,
        skuName: '',
        skuDesc: '',
        weight: '',
        // 写代码收集
        skuAttrValueList: [
        //   {
        //     attrId: 0,
        //     attrName: '',
        //     id: 0,
        //     skuId: 0,
        //     valueId: 0,
        //     valueName: ''
        //   }
        ],
        skuDefaultImg: '',
        skuImageList: [
        //   {
        //     id: 0,
        //     imgName: '',
        //     imgUrl: '',
        //     isDefault: '',
        //     skuId: 0,
        //     spuImgId: 0
        //   }
        ],
        skuSaleAttrValueList: [
        //   {
        //     id: 0,
        //     saleAttrId: 0,
        //     saleAttrName: '',
        //     saleAttrValueId: 0,
        //     saleAttrValueName: '',
        //     skuId: 0,
        //     spuId: 0
        //   }
        ]
      },
      imageList: []
    }
  },
  computed: {
    parent() {
      return this.$parent.$parent
    }
  },
  methods: {
    async  getData(e, category1Id, category2Id, category3Id) {
      this.spuName = e.spuName
      // 加入父组件身上的数据
      this.skuInfo.category3Id = category3Id
      this.skuInfo.spuId = e.id
      this.skuInfo.tmId = e.tmId
      // 获取图片数据
      const res = await this.$api.sku.spuImageList(e.id)
      if (res.code === 200) {
        this.spuImageList = res.data
        this.spuImageList.forEach((item, i) => {
          this.$set(this.spuImageList[i], 'isDefault', 0)
        })
      }
      // 获取销售属性
      const res2 = await this.$api.sku.spuSaleAttrList(e.id)
      if (res2.code === 200) {
        this.spuSaleAttrList = res2.data
      }
      // 获取平台属性
      const res3 = await this.$api.sku.attrInfoList(category1Id, category2Id, category3Id)
      if (res3.code === 200) {
        this.attrInfoList = res3.data
      }
    },
    // 复选框选择
    change(e) {
      this.imageList = e
    },
    // 设置默认(必须要用响应式数组)
    changeDef(row) {
      for (let i = 0; i < this.spuImageList.length; i++) {
        this.$set(this.spuImageList[i], 'isDefault', 0)
      }
      this.$set(row, 'isDefault', 1)
      // 再设置上默认图片
      this.skuInfo.skuDefaultImg = row.imgUrl
    },
    // 取消
    cancel() {
      // 清理data中的值
      Object.assign(this._data, this.$options.data())
      this.parent.scene = 0
    },
    // 保存
    async save() {
      this.attrInfoList.forEach(item => {
        // 整理销售属性
        if (item.attrIdandValueId) {
          const [attrId, valueId] = item.attrIdandValueId.split(':')
          // kv一致省略v,直接加入到数组中
          this.skuInfo.skuAttrValueList.push({ attrId, valueId })
        }
      })
      this.spuSaleAttrList.forEach(item => {
        // 整理销售属性
        if (item.attrIdandValueId) {
          const [attrId, valueId] = item.attrIdandValueId.split(':')
          // kv一致省略v,直接加入到数组中
          this.skuInfo.skuSaleAttrValueList.push({ saleAttrId: attrId, saleAttrValueId: valueId })
        }
      })
      this.imageList.forEach(item => {
        this.skuInfo.skuImageList.push({ imgName: item.imgName, imgUrl: item.imgUrl, isDefault: item.isDefault, spuImgId: item.id })
      })
      const res = await this.$api.sku.saveSkuInfo(this.skuInfo)
      if (res.code === 200) {
        this.$message({ type: 'success', message: '成功' })
        Object.assign(this._data, this.$options.data())
        this.parent.scene = 0
      }
    }
  }
}
</script>

<style lang="sass" scoped>

</style>
